const express = require('express');

const createService = require('../service/create');

const router = express.Router();

// 生成副密码的接口
router.post('/create-sub-card', async function (req, res, next) {
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 获取卡密名称
    let name = req.body.name;
    // 获取卡密到期时间
    let endAt = req.body.endAt || null;   // yyyy-mm-dd hh:mm:ss
    if (!name) {
        return res.status(400).json({
            code: 400,
            msg: "卡密名称不允许为空"
        });
    }
    // 调用业务层生成副卡密
    createService.createSubCard(uid, userTypeId, name, endAt).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

// 生成用户卡密的接口
router.post('/create-user-card', async function (req, res, next) {
    // 获取卡密到期时间 为null 表示不限制时间
    let endAt = req.body.endAt || null;   // yyyy-mm-dd hh:mm:ss
    // 获取卡密名称 为null表示用户卡密不需要名称
    let name = req.body.name || null;
    // 获取操作人信息
    let uid = req.uid;
    let userTypeId = req.userTypeId;
    // 调用业务层生成用户卡密
    createService.createUserCard(uid, userTypeId, name, endAt).then(result => {
        res.status(result.code).json(result);
    }).catch(err => {
        result = { code: 500, msg: "服务器错误", data: null };
    });
});

module.exports = router;